<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<style type="text/css">
#displayNumberTable td{
	border: 1px solid #d8d8d8 ;
	height: 35px;
}
</style>
<!-- 文号的表单项 -->
<input id="displayNumber" readonly="readonly" name="displayNumber" class="easyui-textbox" data-options="onChange:changeDisplayNumberBtn" style="width:60%" type="text"  value="<c:out value='${displayNumber}'/>"/>
<img style="display: none;" id="displayNumberImg" title="点击选择" class="pointer" src="<c:url value='/images/layout.gif'/>" onclick="_openDisplayNumberDiv('',true)"/>
<input id="displayNumberB" name="displayNumberB" class="wenhao" value="删除" disabled="disabled" onclick="deleteDisplayNumber();" type="button" />
<input id="displayGroupId" name="displayGroupId" type="hidden"/>
<input id="displayNumberId" name="displayNumberId" type="hidden"/>
<input id="displayNumberValue" name="displayNumberValue" type="hidden"/>
<input id="displayNumberName" name="displayNumberName" type="hidden"/>
<input id="displayNumberYear" name="displayNumberYear" type="hidden"/>
<!-- 文号弹出框 -->
<div id="displayNumberDiv" style="display: none;">
	<div>文号所属年份：<select id="checkYear" style="width: 160px;"></select> </div>
	<table id="displayNumberTable" class="table" style="width:100%;"></table>
</div>
<!--废号列表弹出框  -->
<div id="unUsedDisplayNumberDiv"></div>
<script type="text/javascript">
	// 用于DocNumberLog表更新用的字段
	var displayGroupId = "";
    var displayNumberId = "";
	var displayNumberValue = "";
	var displayNumberName = "";
	var displayNumberYear = "";
	
	//收文编号特殊处理，去掉年份 start 
	var isReceiveFlow = false;
	if(typeof(_processId) != "undefined" && _processId != null){
		if(new RegExp("DOC_ReceiveFlow").test(_processId) || _processId == "DOC_DeptReadFlow"){
			isReceiveFlow = true;
		}
	}
	//收文编号特殊处理，去掉年份 end
	
	function _openDisplayNumberDiv(checkYear,showDialog)
	{	
		var formId = _formId;
		var text = "<tr><td>行号</td>"
		if(isReceiveFlow){
			text += "<td>文号所属单位</td>";
		}else{
			text += "<td>文号名称</td>";
		}
		//if(!isReceiveFlow){
			text += "<td>年份</td>";
		//}
		text += "<td>编号</td>";
		//text += "<td>操作</td>";
		text += "</tr>";
		$('#displayNumberTable').html(_ajaxLoaderTip);
		$.ajax(
		{  
			type: "POST",
			url:'<c:url value="/synNumber/getDisplayNumber"/>',
			data:"formId="+formId+"&checkYear="+(checkYear==""?$("#checkYear").combobox("getValue"):checkYear),
			async:false,
			//contentType: "application/x-www-form-urlencoded; charset=utf-8",
			success: function(json)
			{	
				//var jsons = JSON.parse(json); 
				var displayNumberList = json.root;			 
				var numberId = "";
				var number = 1;
				for(var i = 0 ; i < displayNumberList.length; i++) { 
					 var hhid = "hh"+i;
					if(numberId == "" || numberId != displayNumberList[i].numberId) {
					
						text = text+"<tr style='width:30px;'><td onclick='checkRadio(this)'><div style='width:50px' ><input id='"+hhid+"' type='radio' name='displayNumberRadio'";
						if(i == 0)
						{
							text = text+" checked='checked'";
						}
						text = text+" />";
						text = text+"<input id='groupId' type='hidden' value='"+displayNumberList[i].groupId+"' />";
						text = text+"<input id='numberId' type='hidden' value='"+displayNumberList[i].numberId+"' />";
						text = text+"<input id='numberName' type='hidden' value='"+displayNumberList[i].numberName+"' />";
						text = text+"<input id='numberValue' type='hidden' value='"+displayNumberList[i].numberValue+"' />";
						text = text+"<input id='numberYear' type='hidden' value='"+displayNumberList[i].numberYear+"' />&nbsp;&nbsp;";
						text = text+number+"</div></td>";
						text = text+"<td><div style='width:100px'>"+displayNumberList[i].numberName+"</div></td>";
						
						//if(!isReceiveFlow){
							text = text+"<td><div id='numberYearColumn' style='width:50px'>"+displayNumberList[i].numberYear+"</div></td>";
						//}
						
						text = text+'<td><div style="width:50px"><input style="width:50px" onchange="checkRadiobyid(this);" vvid="'+hhid+'" class="numberValueSelect" id="numberValueSelect" type="text" value="'+displayNumberList[i].numberValue+'" /></div></td>';
						//text = text+'<td><div style="width:60px"><a href="#" class="easyui-linkbutton" plain="true" onclick="showUnUsed(this);">废号查询</a></div></td>';
						text = text+"</tr>";
						
						numberId = displayNumberList[i].numberId;
						number++;
					}
				}
				/*
				if(isReceiveFlow){
					text = text + "<tr><td onclick='checkRadio(this)'><div style='width:50px' ><input id='sx' type='radio' name='displayNumber' />手写</div></td>";
					text = text + "<td><div style='width:100px'><input id='numberName' vvid='sx' onchange='checkRadiobyid(this);' type='text' style='80px' /><input id='groupId' style='display:none' /></div></td>";
					text = text + "<td><div style='width:30px'><input id='numberValueSelect' vvid='sx' onchange='checkRadiobyid(this);' style='width:50px' type='text' /></div></td></tr>";
					text = text + "<tr><td onclick='checkRadio(this)'><div style='width:50px' ><input type='radio' name='displayNumber' />空</div></td>";
					text = text + "<td><div style='width:100px'><input id='numberName' value='空' type='text' style='display:none'/><input id='groupId' style='display:none' /></div></td>";
					text = text + "<td><div style='width:30px'><input id='numberValueSelect' type='text' style='display:none'/></div></td></tr>";
				}
				*/
			},
			error: function()
			{
				$.messager.alert("提示","文号获取失败！");
			}
        });
        
        $('#displayNumberTable').empty();
        $('#displayNumberTable').append(text);
        $(".numberValueSelect").numberbox({
        	required: true,
    	    min:1,
    	    precision:0
        });
        if(showDialog){
        	$('#displayNumberDiv').dialog({
    			title: "编号",
    			width:550,
    			height:400,
    			buttons: [
    			    {
    			    	text:'确定',	
    			    	handler:function(){
    			    		var obj = $('#displayNumberTable').find(':checked').closest('tr');
    						if(obj.length > 0){
    							displayGroupId = obj.find('#groupId').val();
    							displayNumberId = obj.find('#numberId').val();
    							//displayNumberValue = obj.find('#numberValueSelect option:selected').val();\
    							displayNumberValue = obj.find('#numberValueSelect').val();
    							if(displayNumberValue==""){
    								$.messager.alert("提示","请输入正确的编号"); 
    								return;
    							}
    							displayNumberName = obj.find('#numberName').val();
    							displayNumberYear = obj.find('#numberYear').val();
    							
    							
    						
    	                        var params = {
    	                        	 isreceiveflow:isReceiveFlow,
    	                        	 numberid:displayNumberId,
    							     groupid:displayGroupId,
    					 		     numberyear:displayNumberYear,
    					 		     number:displayNumberValue
    						    };

    							$.ajax(
    		                        { type: "POST",                 
    				                  url: "<c:url value='/synNumber/checkDisplayNumber'/>",
    				                  data:"paramsJson="+encodeURIComponent(encodeURIComponent(JSON.stringify(params))),
    				                  async:false,
    				                  success: function(json) {
    					                 var checknumber =  json.checknumber;
    				                     if("true" == checknumber){
    				                    	 $.messager.alert("提示","文号已被占用,请更换后再试"); 
    				                        return ;
    				                     }
    				                     if(isReceiveFlow){
    				                    	 $("#displayNumber").textbox("setValue", displayNumberName+"〔"+displayNumberYear+"〕"+displayNumberValue+"号");
    								         //$('#displayNumber').val(displayNumberValue);
    							         } else {
    							        	
    							              if('辽烟通报'== displayGroupId || '辽烟信息' == displayGroupId){
    							                  //$('#displayNumber').val(displayNumberName+" "+displayNumberYear+" 第 "+displayNumberValue+" 期");
    							            	  $("#displayNumber").textbox("setValue", displayNumberName+" "+displayNumberYear+" 第 "+displayNumberValue+" 期");
    							              }else if('培训'== displayGroupId ){
    							                  //$('#displayNumber').val(displayNumberName);
    							            	  $("#displayNumber").textbox("setValue", displayNumberName);
    							              }else if('信访登记'== displayGroupId ){
    							                  //$('#displayNumber').val(displayNumberName+"〔"+displayNumberYear+"〕第"+displayNumberValue+"号");
    							            	  $("#displayNumber").textbox("setValue", displayNumberName+"〔"+displayNumberYear+"〕第"+displayNumberValue+"号");
    							              }else if('会议纪要'==displayGroupId||'总公司会议纪要'==displayGroupId){
    							            	  $("#displayNumber").textbox("setValue", "第 "+displayNumberValue+" 期"); 
    							              }else if('工作简报'==displayGroupId){
    							            	  $("#displayNumber").textbox("setValue", "第 "+displayNumberValue+" 期"); 
    							              }else{
    								            //$('#displayNumber').val(displayNumberName+"〔"+displayNumberYear+"〕"+displayNumberValue+"号");
    								            $("#displayNumber").textbox("setValue", displayNumberName+"〔"+displayNumberYear+"〕"+displayNumberValue+"号");
    							              }
    							         }
    				                   		//from表单提交
    		    							$("#displayGroupId").val(displayGroupId);
    		    							$("#displayNumberId").val(displayNumberId);
    		    							$("#displayNumberValue").val(displayNumberValue);
    		    							$("#displayNumberName").val(displayNumberName);
    		    							$("#displayNumberYear").val(displayNumberYear);
    		    							
    		    							if(_issueId!=""){
    											_saveOAdata();
    										}
    		    							
    							         //使用前台方式回填编号
    							        // _setDisplayNumber($('#displayNumber').val());
    	                                 // 关闭弹出层
    						             $("#displayNumberDiv").dialog( "close" );
    				                  },
    				                  error: function() {
    				                	  $.messager.alert("提示","验证异常，请联系管理员！");
    				                  }
    			                 });			
    						}
    			    	}
    			    },
    				{
    					text:'关闭',
    					handler:function(){
    						$('#displayNumberDiv').dialog("close");
    					}
    				}
    			]
    		});
        }
	}

	// 删除文号
	function deleteDisplayNumber()
	{
		var params = 
					{
						docId:$('#docId').val(),
				 		//numberContent:$('#displayNumber').val()
				 		numberContent:$("#displayNumber").textbox('getValue')
					};
		$.ajax(
		{
			type: "POST",                 
			url: "<c:url value='/synNumber/deleteDisplayNumber'/>",
			data:"paramsJson="+JSON.stringify(params),
			async:false,
			success: function(json){
				// 清空编号相关信息
				$('#displayNumber').textbox("setValue","");
				$("#displayGroupId,#displayNumberId,#displayNumberValue,#displayNumberName,#displayNumberYear").val("");
				//_setDisplayNumber("");
				displayGroupId = "";
			    displayNumberId = "";
				displayNumberValue = "";
				displayNumberName = "";
				displayNumberYear = "";

				if(_issueId!=""){
					_saveOAdata();
				}
			},
			error: function()
			{
				$.messager.alert("提示","删除编号失败！");
			}
		});
	}
var isNumbertemp =$("#isNumber").val();
	/*将控制文号权限的方法，从原先散落在各个发文页面集中到本页面*/
	//if(typeof(_docType)!="undefined"){
	//	if(_docType == ""){
			if(actionPermissionJson.DirNumber == "true" && _issueType=="todo" && 'yes'==isNumbertemp){
				//文号有权限
				if("<c:out value='${displayNumber}'/>"==""){
					$('#displayNumberImg').show();
					$('#displayNumberB').attr("disabled",true);
				}else{
					$('#displayNumberImg').hide();
					$('#displayNumberB').attr("disabled",false);
				}
		    }
	  //  }
	//}
	
	//点击选择按钮所在TD即选中相应按钮 start
	function checkRadio(obj){
 
		$(obj).find("input[type='radio']").attr('checked','checked')
	}
	//根据id选择radio
    function checkRadiobyid(obj){
	    
		 $("#"+obj.vvid).attr('checked','checked')
	}
	//点击选择按钮所在TD即选中相应按钮 end
	$(function(){
		if(actionPermissionJson.DirNumber == "true" && _issueType=="todo"){
			$('#unUsedDisplayNumberDiv').dialog({
				title:"废号列表",//标题
				width: 300,//宽度
			    height: 400,//高度
			    top:10,
			    closed: true,//初始关闭
			    cache: true,//缓存
			    modal: true,
			    href: ''
			});
			
			$("#checkYear").combobox({
			    url:'<c:url value="/synNumber/getYears" />',
			    valueField:'id',
			    textField:'text',
			    editable:false,
			    onChange:function(newValue,oldValue){
			    	_openDisplayNumberDiv(newValue,false);
			    }
			});
		}
	});
	function showUnUsed(control){
		var numberId=$(control).closest('tr').find('#numberId').val(); 
		$('#unUsedDisplayNumberDiv').dialog("refresh",'<c:url value="/synNumber/unusedDisplayNumber" />?numberId='+numberId).dialog("open");
	}
	function changeDisplayNumberBtn(newValue,oldValue){
		if(newValue==""){
			$('#displayNumberImg').show();
			$('#displayNumberB').attr("disabled",true);
		}else{
			$('#displayNumberImg').hide();
			$('#displayNumberB').attr("disabled",false);
		}
	}
</script>